package com.fighYo.trace.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fighYo.trace.entity.Admin;
import com.fighYo.trace.service.AdminService;
import com.fighYo.trace.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fighYo
 * @since 2022-04-16
 */
@Api(tags = {"管理员管理"})
@CrossOrigin
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @ApiOperation("登录")
    @PostMapping("/login")
    public R login(@RequestBody Admin admin) {
        String userName = admin.getUserName();
        String password = admin.getPassword();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_name", userName);
        wrapper.eq("password", password);
        Admin user = adminService.getOne(wrapper);
        if (user == null) {
            return R.error().msg("身份验证失败");
        } else {
            return R.ok().data("token", "admin");
        }
    }

    @ApiOperation("获取身份信息")
    @GetMapping("/info")
    public R info() {
        return R.ok().data("roles", "[admin]")
                .data("name", "admin")
                .data("avatar", "https://guli-aaa.oss-cn-hangzhou.aliyuncs.com/cat.jpeg");
    }

    @ApiOperation("登出")
    @GetMapping("logout")
    public R logout() {
        return R.ok();
    }


}

